package com.syni.mdd.sjb.tool.controller.protect;

import com.syni.mdd.sjb.common.component.mybatis.common.entity.DxshAppUserInfo;
import com.syni.mdd.sjb.common.component.utils.common.annotations.CheckUserIdentity;
import com.syni.mdd.sjb.common.component.utils.common.enums.CommonResultEnum;
import com.syni.mdd.sjb.common.component.utils.common.utils.ResultMapHelper;
import com.syni.mdd.sjb.tool.entity.vo.requestVo.UserBindWxVo;
import com.syni.mdd.sjb.tool.entity.vo.responseVo.UserBindWxRpVo;
import com.syni.mdd.sjb.tool.service.DxshAppUserInfoService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

@RestController
@RequestMapping("/user")
public class UserCommonController {

    @Autowired
    DxshAppUserInfoService dxshAppUserInfoService;

    @PostMapping("/bindWx")
    @ApiOperation(value = "绑定微信")
    @CheckUserIdentity
    public Map<String,Object> bindWx(@RequestBody UserBindWxVo userBindWxVo){
        //判断这个unionid是否已经绑定其他手机号了
        DxshAppUserInfo dxshAppUserInfo = dxshAppUserInfoService.getByUniondId(userBindWxVo.getUnionId());
        if(dxshAppUserInfo != null && !dxshAppUserInfo.getUserId().equals(userBindWxVo.getUserId())){
            return ResultMapHelper.result(CommonResultEnum.WX_BIND_EXIST);
        }
        boolean ret = dxshAppUserInfoService.bindWx(userBindWxVo.getUserId(),userBindWxVo.getUnionId());
        if(ret){
            UserBindWxRpVo userBindWxRpVo = new UserBindWxRpVo();
            userBindWxRpVo.setUnionid(userBindWxVo.getUnionId());
            return ResultMapHelper.success(userBindWxRpVo);
        }
        return ResultMapHelper.result(CommonResultEnum.WX_BIND_FAIL);
    }

    @PostMapping("/appBindWx")
    @ApiOperation(value = "绑定微信")
    @CheckUserIdentity
    public Map<String,Object> appBindWx(@RequestBody UserBindWxVo userBindWxVo){
        return dxshAppUserInfoService.appBindWx(userBindWxVo);
    }
}
